package com.dykj.fireFighting.web.app;

import java.util.*;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.dykj.fireFighting.api.base.maintenanceReply.entity.MaintenanceReply;
import com.dykj.fireFighting.api.base.maintenanceReply.service.IMaintenanceReplyService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;



/**
 * 回复表接口
 *
 * @Author JacksonTu
 * @Date  2021-09-01 08:36:19
 */
@Slf4j
@Api(value = "回复表接口", tags = {"回复表接口"})
@RestController
@RequestMapping("app/maintenanceReply")
public class AppMaintenanceReplyController {

    @DubboReference
    private IMaintenanceReplyService maintenanceReplyService;


    /**
     * 回复表列表
     *
     * @return
     */
    @AutoLog(value = "回复表-分页列表查询")
    @ApiOperation(value = "回复表分页列表", notes = "回复表分页列表")
    @GetMapping("/list")
    @RequiresPermissions("app/maintenanceReply/list")
    public CommonResult<PageVo<MaintenanceReply>> treeGrid(CommonDto commonDto) {
        PageVo<MaintenanceReply> page = maintenanceReplyService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取回复表信息
     */
    @AutoLog(value = "回复表-获取回复表信息")
    @ApiOperation(value = "获取回复表信息", notes = "获取回复表信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
    //@RequiresPermissions("app/maintenanceReply/info")
    public CommonResult<MaintenanceReply> info(@PathVariable("id") String id){
        MaintenanceReply maintenanceReply = maintenanceReplyService.getById(id);
        return CommonResult.success(maintenanceReply);
    }

    /**
     * 保存回复表信息
     */
    @AutoLog(value = "回复表-保存回复表信息")
    @ApiOperation(value = "保存回复表信息", notes = "保存回复表信息")
    @PostMapping("/save")
    @RequiresPermissions("app/maintenanceReply/save")
    public CommonResult save(@Valid @RequestBody MaintenanceReply maintenanceReply) {
        try {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            maintenanceReply.setId(UUIDUtils.get32UUID());
            maintenanceReply.setCreateBy(loginUserVo.getId().toString());
            maintenanceReply.setCreateTime(new Date());
            maintenanceReplyService.save(maintenanceReply);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改回复表信息
     */
    @AutoLog(value = "回复表-修改回复表信息")
    @ApiOperation(value = "修改回复表信息", notes = "修改回复表信息")
    @PostMapping("/update")
    //@RequiresPermissions("app/maintenanceReply/update")
    public CommonResult update(@Valid @RequestBody MaintenanceReply maintenanceReply) {
        try {
            LoginUserVo loginUserVo = ShiroUtil.getUser();
            maintenanceReply.setUpdateBy(loginUserVo.getId().toString());
            maintenanceReply.setUpdateTime(new Date());
            maintenanceReplyService.updateById(maintenanceReply);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除回复表信息
     */
    @AutoLog(value = "回复表-删除回复表信息")
    @ApiOperation(value = "删除回复表信息", notes = "删除回复表信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
    @RequiresPermissions("app/maintenanceReply/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                maintenanceReplyService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
}
